﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class SubjectAbility : System.Web.UI.Page
    {
        private int SubjectID = 0, editid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterKnow"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["subjectid"] == null)
            {
                Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
            }
            else
            {
                if (!Utility.chk_int(Request.QueryString["subjectid"]))
                {
                    Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
                }
                else
                {
                    SubjectID = Convert.ToInt32(Request.QueryString["subjectid"].Trim());
                }
            }
            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    SubjectAbilityInfo subjectAbilityInfo = BAL.SubjectAbility.GetAbilityInfoById(editid);
                    AbilityName.Text = subjectAbilityInfo.Name;
                    AbilityKeyword.Text = subjectAbilityInfo.Keyword;
                }
                LoadList();
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (AbilityName.Text.Trim() == "")
                {
                    Utility.ShowJsAlert("请填写知识点名称", this);
                }
                else if (AbilityKeyword.Text.Trim() == "")
                {
                    Utility.ShowJsAlert("请填写知识点关键字", this);
                }
                else
                {
                    SubjectAbilityInfo subjectAbilityInfo = new SubjectAbilityInfo();

                    if (editid == 0)
                    {
                        subjectAbilityInfo.SubjectId = SubjectID;
                    }
                    else
                    {
                        subjectAbilityInfo = BAL.SubjectAbility.GetAbilityInfoById(editid);
                    }
                    subjectAbilityInfo.Name = AbilityName.Text.Trim();
                    subjectAbilityInfo.Keyword = AbilityKeyword.Text;
                    subjectAbilityInfo.SubjectId = SubjectID;
                    if (editid > 0)
                    {
                        if (BAL.SubjectAbility.CheckAbilityExistByKeyword(subjectAbilityInfo.Keyword, editid, SubjectID))
                        {
                            Utility.ShowJsAlert(subjectAbilityInfo.Keyword + "-" + editid.ToString(), this);
                        }
                        else
                        {
                            BAL.SubjectAbility.EditAbility(subjectAbilityInfo);
                            editid = 0;
                            Response.Redirect("SubjectAbility.aspx?subjectid=" + SubjectID);
                        }
                    }
                    else
                    {
                        if (BAL.SubjectAbility.CheckAbilityExistByKeyword(subjectAbilityInfo.Keyword, editid, SubjectID))
                        {
                            Utility.ShowJsAlert("知识点已存在，请勿重复添加", this);
                        }
                        else
                        {
                            BAL.SubjectAbility.AddAbility(subjectAbilityInfo);
                            AbilityKeyword.Text = "";
                            AbilityName.Text = "";
                            LoadList();
                        }
                    }
                }

            }
            catch
            {
                Utility.ShowJsAlert("保存失败", this);
            }
        }
        protected void LoadList()
        {
            IList<SubjectAbilityInfo> subjectAbilityInfos = BAL.SubjectAbility.GetAbilityInfosBySubjectId(SubjectID);
            this.AbilityList.Items.Clear();
            foreach (SubjectAbilityInfo subjectAbilityInfo in subjectAbilityInfos)
            {
                ListItem item = new ListItem(subjectAbilityInfo.Name, subjectAbilityInfo.Id.ToString());
                AbilityList.Items.Add(item);
            }
        }
        protected void DelBut_Click(object sender, EventArgs e)
        {
            if (AbilityList.SelectedValue == "")
            {
                Utility.ShowJsAlert("请选择要删除的项", this);
            }
            else
            {
                int ID = int.Parse(AbilityList.SelectedValue.ToString());
                BAL.SubjectAbility.DelAbility(ID);
                LoadList();
            }
        }

        protected void EditBut_Click(object sender, EventArgs e)
        {
            if (AbilityList.SelectedValue == "")
            {
                Utility.ShowJsAlert("请选择要编辑的知识点", this);
            }
            else
            {
                int ID = int.Parse(AbilityList.SelectedValue.ToString());
                Response.Redirect("SubjectAbility.aspx?subjectid=" + SubjectID + "&editid=" + ID);
            }
        }
    }
}
